package com.cwd.arm.common.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

// TODO: 22-3-13 修改 signName
/**
 * Created by yan on 22-3-12.
 */
@Component
public class SendSmsUtil {

    private static Client client;

    @Value("${access.key.id}")
    private String accessKeyId;

    @Value("${access.key.secret}")
    private String accessKeySecret;

    @Value("${msg.endpoint}")
    private String msgEndpoint;

    private String signName = "";

    public void send(String templateCode,String phoneNumbers,String templateParam) throws Exception {
        Client client = createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phoneNumbers)
                .setTemplateParam(templateParam);
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
    }

    private Client createClient() throws Exception {
        if (client == null) {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId)
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = msgEndpoint;
            client = new Client(config);
        }
        return client;
    }
}
